package com.sskj.mine.ui.activity;


import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jakewharton.rxbinding2.widget.RxAdapterView;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.box.spinneradapter.ArrowAdapter;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.presenter.PayTypeUpdateActivityPresenter;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

import static android.app.Activity.RESULT_OK;

@Route(path = RConfig.MINE_PAY_TYPE_UPDATE)//支付方式添加修改弹窗
public class PayTypeUpdateActivity extends BaseActivity<PayTypeUpdateActivityPresenter> {

    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.spinner)
    Spinner spinner;
    @BindView(R2.id.tvName)
    TextView tvName;
    @BindView(R2.id.tvBank)
    TextView tvBank;
    @BindView(R2.id.etBank)
    EditText etBank;
    @BindView(R2.id.tvBankBranch)
    TextView tvBankBranch;
    @BindView(R2.id.etBankBranch)
    EditText etBankBranch;
    @BindView(R2.id.etBankCardNum)
    EditText etBankCardNum;
    @BindView(R2.id.llBank)
    LinearLayout llBank;
    @BindView(R2.id.tvAccount)
    TextView tvAccount;
    @BindView(R2.id.etAccount)
    EditText etAccount;
    @BindView(R2.id.tvTip)
    TextView tvTip;
    @BindView(R2.id.llOther)
    LinearLayout llOther;
    @BindView(R2.id.tvCancel)
    TextView tvCancel;
    @BindView(R2.id.tvSure)
    TextView tvSure;
    @BindView(R2.id.etPayPwd)
    EditText etPayPwd;
    @BindView(R2.id.rlImg)
    RelativeLayout rlImg;
    @BindView(R2.id.ivQRCode)
    ImageView ivQRCode;
    @BindView(R2.id.llUpload)
    LinearLayout llUpload;
    @BindView(R2.id.tvQRCodeTitle)
    TextView tvQRCodeTitle;
    @Autowired
    int initPos;
    private int curPos = -1;
    @Autowired
    boolean isAdd;
    @Autowired
    String payId;
    private String payType = "wx";
    private String act;
    private String imgUrl;
    private String mImagePath;
    @Inject
    UserViewModel userViewModel;

    @Override
    protected int getLayoutResId() {
        return R.layout.mine_activity_pay_type_update;
    }

    @Override
    public boolean getOrientation() {
        return false;
    }

    @Override
    public PayTypeUpdateActivityPresenter getPresenter() {
        return new PayTypeUpdateActivityPresenter();
    }

    @Override
    protected void initView() {
        DaggerUserDataComponent.create().inject(this);
        ARouter.getInstance().inject(this);
        if (isAdd) {
            tvTitle.setText(R.string.mine_add_pay_type);
            act = "add";
        } else {
            tvTitle.setText(R.string.mine_more_pay_type);
            spinner.setEnabled(false);
            act = "edit";
        }
        if (!TextUtils.isEmpty(payId)) {
            payType = payId;
            switch (payId) {
                case "wx":
                    initPos = 0;
                    break;
                case "alipay":
                    initPos = 1;
                    break;
                case "paypal":
                    initPos = 2;
                    break;
                case "backcard":
                    initPos = 3;
                    break;
            }
        }
        List<String> payTypes = Arrays.asList(getResources().getStringArray(R.array.mine_pay_types));
        ArrowAdapter spinnerAdapter = new ArrowAdapter(this,
                R.layout.mine_item_pay_type, payTypes);
        spinnerAdapter.setDropDownViewResource(R.layout.mine_item_pay_type_drop);
        spinner.setAdapter(spinnerAdapter);
        spinner.setSelection(initPos);
        RxAdapterView.itemSelections(spinner)
                .subscribe(pos -> {
                    if (pos != curPos) {
                        curPos = pos;
                        if (curPos > 2) {
                            llBank.setVisibility(View.VISIBLE);
                            llOther.setVisibility(View.GONE);
                            payType = "backcard";
                        } else {
                            llBank.setVisibility(View.GONE);
                            llOther.setVisibility(View.VISIBLE);
                            imgUrl = null;
                            ivQRCode.setVisibility(View.GONE);
                            llUpload.setVisibility(View.VISIBLE);

                            if (curPos == 0) {
                                payType = "wx";
                                changeWeixin();
                            } else if (curPos == 1) {
                                payType = "alipay";
                                changeAlipay();
                            } else if (curPos == 2) {
                                payType = "paypal";
                                changePaypal();
                            }
                        }
                    }
                });
        ClickUtil.click(tvCancel, this::onBackPressed);
        ClickUtil.click(tvSure, () -> {
            if (checkParam()) {
                mPresenter.addPayWay(act,//添加/编辑
                        payType,//支付方式
                        etPayPwd.getText().toString(),//支付密码
                        etAccount.getText().toString(),
                        imgUrl,
                        etAccount.getText().toString(),
                        imgUrl,
                        etBankCardNum.getText().toString(),//银行卡号
                        etBankBranch.getText().toString(),//开户支行
                        etBank.getText().toString(),//开户行
                        tvName.getText().toString().trim(),//用户名称
                        etAccount.getText().toString());//paypel账户
            }
        });
        ClickUtil.click(rlImg, () -> {
            PictureSelector.create(this)
                    .openGallery(PictureMimeType.ofImage())
                    .selectionMode(PictureConfig.SINGLE)
                    .compress(true)
                    .forResult(PictureConfig.CHOOSE_REQUEST);
        });
    }

    private boolean checkParam() {
        switch (curPos) {
            case 0://微信
                if (TextUtils.isEmpty(etAccount.getText())) {
                    ToastUtil.showShort(R.string.mine_pay_type1);
                    return false;
                }
                if (imgUrl == null) {
                    ToastUtil.showShort(R.string.mine_pay_type2);
                    return false;
                }
                if (TextUtils.isEmpty(etPayPwd.getText())) {
                    ToastUtil.showShort(R.string.mine_pay_type3);
                    return false;
                }
                if (!CheckUtil.isPayPwd(etPayPwd.getText().toString())) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mineSetPayPwdActivity4));
                    return false;
                }
                break;
            case 1://支付宝
                if (TextUtils.isEmpty(etAccount.getText())) {
                    ToastUtil.showShort(R.string.mine_pay_type4);
                    return false;
                }
                if (imgUrl == null) {
                    ToastUtil.showShort(R.string.mine_pay_type5);
                    return false;
                }
                if (TextUtils.isEmpty(etPayPwd.getText())) {
                    ToastUtil.showShort(R.string.mine_pay_type6);
                    return false;
                }
                if (!CheckUtil.isPayPwd(etPayPwd.getText().toString())) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mineSetPayPwdActivity4));
                    return false;
                }
                break;
            case 2:
                if (TextUtils.isEmpty(etAccount.getText())) {
                    ToastUtil.showShort(R.string.mine_pay_type0);
                    return false;
                }
                if (TextUtils.isEmpty(etPayPwd.getText())) {
                    ToastUtil.showShort(R.string.mine_pay_type6);
                    return false;
                }
                if (!CheckUtil.isPayPwd(etPayPwd.getText().toString())) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mineSetPayPwdActivity4));
                    return false;
                }
                break;
            case 3:
                if (TextUtils.isEmpty(etBank.getText())) {
                    ToastUtil.showShort(R.string.mine_pay_type7);
                    return false;
                }
                if (TextUtils.isEmpty(etBankBranch.getText())) {
                    ToastUtil.showShort(R.string.mine_pay_type8);
                    return false;
                }
                if (TextUtils.isEmpty(etBankCardNum.getText())) {
                    ToastUtil.showShort(R.string.mine_pay_type9);
                    return false;
                }
                if (TextUtils.isEmpty(etPayPwd.getText())) {
                    ToastUtil.showShort(R.string.mine_pay_type6);
                    return false;
                }
                if (!CheckUtil.isPayPwd(etPayPwd.getText().toString())) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mineSetPayPwdActivity4));
                    return false;
                }
                break;
        }

        return true;
    }

    @Override
    protected void initData() {
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                UserData userData = users.get(0);
                tvName.setText(userData.getRealname());
            }
        });
    }

    /**
     * 更改为微信界面
     */
    private void changeWeixin() {
        imgUrl = null;
        etAccount.setText("");
        tvAccount.setText(R.string.mine_wechat_account);
        etAccount.setHint(R.string.mine_wechat_account_input);
        tvTip.setText(R.string.mine_upload_wechat_img);
        tvQRCodeTitle.setVisibility(View.VISIBLE);
        rlImg.setVisibility(View.VISIBLE);
    }

    /**
     * 更改为支付宝页面
     */
    private void changeAlipay() {
        imgUrl = null;
        etAccount.setText("");
        tvAccount.setText(R.string.mine_alipay_account);
        etAccount.setHint(R.string.mine_alipay_account_input);
        tvTip.setText(R.string.mine_upload_alipay_img);
        tvQRCodeTitle.setVisibility(View.VISIBLE);
        rlImg.setVisibility(View.VISIBLE);
    }

    /**
     * 更改为Paypal界面
     */
    private void changePaypal() {
        imgUrl = null;
        etAccount.setText("");
        tvAccount.setText("Paypal");
        etAccount.setHint("请输入您的Paypal账号");
        tvQRCodeTitle.setVisibility(View.GONE);
        rlImg.setVisibility(View.GONE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 图片、视频、音频选择结果回调
        if (resultCode == RESULT_OK && requestCode == PictureConfig.CHOOSE_REQUEST) {
            List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
            mImagePath = TextUtils.isEmpty(selectList.get(0).getCompressPath()) ? selectList.get(0).getPath() : selectList.get(0).getCompressPath();
            mPresenter.uploadImg(new File(mImagePath));
            llUpload.setVisibility(View.GONE);
        }
    }

    public void uploadSuccess(String imgUrl) {
        this.imgUrl = imgUrl;
        ivQRCode.setVisibility(View.VISIBLE);
        ImageUtil.setImage(mImagePath, ivQRCode);
    }

    public void addSuccess() {
        userViewModel.update();
        finish();
    }
}
